﻿using MediatR;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.API.Applcation.Command.InjureCommand;
using Warehouse.API.Applcation.Command.Movelibrary;
using Warehouse.API.Applcation.Command.MovelibraryCommand;
using Warehouse.Domain.DTO;

namespace Warehouse.API.Controllers
{
    /// <summary>
    /// 移库控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MovelibraryController : ControllerBase
    {
        private readonly IMediator _mediator;

        public MovelibraryController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加移库管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMovelirary(AddMovelibraryDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.AllProducs.FirstOrDefault().office_id = sid;
                command.AllProducs.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Data = await _mediator.Send(new MovelibraryCreateCommand { addDto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改移库审核状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStateMovelibrary(MovelibraryAuditCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.user_id = Convert.ToInt32(uid);
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除移库产品管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelMovelibrary(MovelibraryDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改移库管理
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdMovelibrary(UpdMovelibraryDTO comm)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                comm.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                comm.office_id = sid;
                comm.updAllProducs.FirstOrDefault().office_id = sid;
                comm.updAllProducs.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Data = await _mediator.Send(new MovelibraryUpdCommand { updDto = comm });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
